package com.marktotop.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.marktotop.entity.system.Log;
import com.marktotop.entity.system.LoginLog;
import com.marktotop.mapper.LoginLogMapper;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.ILoginLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.marktotop.utils.AddressUtil;
import com.marktotop.utils.IPUtil;
import com.marktotop.vo.system.ActiveUserVO;
import eu.bitwalker.useragentutils.Browser;
import eu.bitwalker.useragentutils.OperatingSystem;
import eu.bitwalker.useragentutils.UserAgent;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 登录日志表 服务实现类
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@Service
public class LoginLogServiceImpl extends ServiceImpl<LoginLogMapper, LoginLog> implements ILoginLogService {

    @Autowired
    private LoginLogMapper loginLogMapper;

    /**
     * 插入登入日志
     *     rollbackFor = Exception.class 如果类加了这个注解,那
     *     么这个类里面的方法抛出异常,就会回滚,数据库里面的数据也会回滚。
     * @param request
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(HttpServletRequest request) {
        loginLogMapper.insert(createLoginLog(request));
    }


    /**
     * 创建登入日志
     * @param
     * @return
     */
    private static LoginLog createLoginLog(HttpServletRequest request) {
        ActiveUserVO activeUser = (ActiveUserVO) SecurityUtils.getSubject().getPrincipal();
        LoginLog loginLog = new LoginLog();
        loginLog.setUsername(activeUser.getUser().getUsername());
        loginLog.setIp(IPUtil.getIpAddr(request));
        loginLog.setLocation(AddressUtil.getCityInfo(IPUtil.getIpAddr(request)));
        // 获取客户端操作系统
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
        Browser browser = userAgent.getBrowser();
        OperatingSystem os = userAgent.getOperatingSystem();
        loginLog.setUserSystem(os.getName());
        loginLog.setUserBrowser(browser.getName());
        loginLog.setLoginTime(new Date());
        return loginLog;
    }


    /**
     * 分页展示 登录日志列表
     *
     * @param pageNum
     * @param pageSize
     * @param loginLog
     * @return
     */
    @Override
    public ResultBean findLoginLogList(Integer pageNum, Integer pageSize, LoginLog loginLog) {
        // 开启分页
        Page<LoginLog> page = new Page<>(pageNum,pageSize);
        IPage<LoginLog> loginLogsByPage = loginLogMapper.getLoginLogsByPage(page,loginLog);
        if (loginLogsByPage.getRecords().isEmpty()){
            return ResultBean.result(ResultCode.NON_RESULT);
        }
        return ResultBean.ok().message("获取登录日志信息成功！")
                .data("total",loginLogsByPage.getTotal())
                .data("loginLogs",loginLogsByPage.getRecords());
    }
}
